package com.lj.universalframework.widget

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

/**
 * <p>文件名：PressedTextView
 * <p>创建者：LJ
 * <p>创建日期：2020\10\16 0016 14:30
 * <p>描述：
 */
class PressedTextView:AppCompatTextView {

    private var pressedScale = 0f
    private var set: AnimatorSet? = null
    private var pressedFlag = 0

    constructor(context: Context) : super(context) {
        pressedScale = 1.1f
        pressedFlag = 1
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context,attrs) {
        pressedScale = 1.1f
        pressedFlag = 1
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context,attrs,defStyleAttr) {
        pressedScale = 1.1f
        pressedFlag = 1
    }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        if (isPressed) {
            pressedFlag = 1
            if (null == set) {
                set = AnimatorSet()
                set!!.duration = 5
            }
            if (set!!.isRunning) set!!.cancel()
            val pScaleX = ObjectAnimator.ofFloat(this, "scaleX", 1.0f, pressedScale)
            val pScaleY = ObjectAnimator.ofFloat(this, "scaleY", 1.0f, pressedScale)
            set!!.play(pScaleX).with(pScaleY)
            set!!.start()
        } else {
            if (pressedFlag != 1) {
                return
            }
            pressedFlag = 2
            if (null == set) {
                set = AnimatorSet()
                set!!.duration = 5
            }
            if (set!!.isRunning) set!!.cancel()
            val nScaleX = ObjectAnimator.ofFloat(this, "scaleX", pressedScale, 1.0f)
            val nScaleY = ObjectAnimator.ofFloat(this, "scaleY", pressedScale, 1.0f)
            set!!.play(nScaleX).with(nScaleY)
            set!!.start()
        }
    }
}